package com.example.lin.day9_3;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.HeaderViewHolder> {

    private Context context;
    private List<RVItem> list;

    public List<RVItem> getList() {
        return list;
    }

    public void setList(List<RVItem> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public MyRecyclerAdapter(Context context) {
        this.context = context;
    }

    /**
     * 创建 ViewHolder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public HeaderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.view_rv_item_header, parent, false);
        return new HeaderViewHolder(itemView);
    }

    /**
     * 为每个ViewHolder绑定 数据
     *
     * @param holder   Item 的 ViewHolder
     * @param position Item 的下标
     */
    @Override
    public void onBindViewHolder(@NonNull HeaderViewHolder holder, int position) {
        RVItem rvItem = list.get(position);
        holder.iv_icon.setImageResource(rvItem.getImageResId());
        holder.tv_msg.setText(rvItem.getMsg());
    }

    /**
     * 确定 Item 的数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * 一个ViewHolder 决定了一个Item类型的布局
     */
    class HeaderViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_icon;
        TextView tv_msg;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_msg = itemView.findViewById(R.id.tv_msg);
        }
    }

}
